﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class FormDanhSachBanAn : Form
    {
        public FormDanhSachBanAn()
        {
            InitializeComponent();
            KhuVucBUS bus = new KhuVucBUS();
            List<KhuVucDTO> danhsach = new List<KhuVucDTO>();
            danhsach = bus.LayDanhSachKhuVuc();
            ccbChonKhuVuc.DataSource = danhsach;
            ccbChonKhuVuc.DisplayMember = "Ten_Khu_Vuc";
            ccbChonKhuVuc.ValueMember = "Id_Khu_Vuc";
        }

        private void btnThemBanAn_Click(object sender, EventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormDanhSachBanAn_Load(object sender, EventArgs e)
        {
            
        }

        private void chkChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            if (chkChonTatCa.Checked)
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachBanAn.Rows)
                {
                    r.Cells[4].Value = true;
                }
            }
            else if (!(chkChonTatCa.Checked))
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachBanAn.Rows)
                {
                    r.Cells[4].Value = false;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Bạn Chắc Chắn Muốn Xóa Bàn Ăn Này?", "Xóa Bàn Ăn", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>();
                foreach (DataGridViewRow row in gridViewXemDanhSachBanAn.Rows)
                {
                    DataGridViewCheckBoxCell chk = row.Cells[3] as DataGridViewCheckBoxCell;
                    if (Convert.ToBoolean(chk.Value) == true)
                        rowsToDelete.Add(row);
                }
                //loop through the list to delete rows added to list<T>:
                foreach (DataGridViewRow row in rowsToDelete)
                {
                    BanAnBUS tk = new BanAnBUS();
                    tk.XoaBanAn(Convert.ToInt32(row.Cells[1].Value));
                    gridViewXemDanhSachBanAn.Rows.Remove(row);
                }
            }
        }

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
           
            
        }
        public static string Ten_Ban_An = "";
        public static int ID;
        private void btnSua_Click(object sender, EventArgs e)
        {
            int chkDem = 0;
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachBanAn.Rows)
            {
                if (singleRow.Cells[4].Value != null)
                {
                    if ((Boolean)singleRow.Cells[4].Value == true)
                    {
                        chkDem = chkDem + 1;
                    }
                }
            }
            if (chkDem == 0 || chkDem > 1)
            {
                MessageBox.Show("Chỉ Được Chọn 1 Bàn Ăn Để Tiến Hành Sửa Đổi!");
                return;
            }
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachBanAn.Rows)
            {
                if (singleRow.Cells[4].Value != null)
                {
                    if ((Boolean)singleRow.Cells[4].Value == true)
                    {
                        ID = Convert.ToInt32(singleRow.Cells[1].Value.ToString());
                        Ten_Ban_An = singleRow.Cells[2].Value.ToString();
                    }
                }
            }
            FormCapNhatBanAn f = new FormCapNhatBanAn();
            f.ShowDialog();
        }

        private void gridViewXemDanhSachBanAn_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ccbChonKhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridViewXemDanhSachBanAn.Rows.Clear();
            int _ID = int.Parse(ccbChonKhuVuc.SelectedValue.ToString());
            List<BanAnDTO> danhsach = new List<BanAnDTO>();
            BanAnBUS bus = new BanAnBUS();
            danhsach = bus.LayDanhSachBanAnTheoKhuVuc(_ID);
          
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachBanAn.Rows.Add(i + 1, danhsach[i].Id_Ban_An, danhsach[i].Ten_Ban_An, danhsach[i].Id_Khu_Vuc);
            }
        }

        
    }
}
